#include "SymbolTable.h"

SymbolTable::SymbolTable(SymbolTable *i_ParentTable)
{
	this->m_ParentTable = i_ParentTable;
}

SymbolTable::~SymbolTable(void)
{
}

SymbolTableEntry *SymbolTable::Add(const Token *i_Token)
{
	SymbolTableEntry *entry = new SymbolTableEntry(i_Token);

	if (!this->m_HashTable.Add(i_Token->lexeme, entry))
	{
		delete entry;
		entry = NULL;
	}

	return entry;
}

SymbolTableEntry *SymbolTable::Lookup(char *i_Name) const
{
	return this->m_HashTable.GetValue(i_Name);
}

SymbolTableEntry *SymbolTable::Find(char *i_Name) const
{
	SymbolTableEntry *entry = this->m_HashTable.GetValue(i_Name);

	if (!entry && this->m_ParentTable)
	{
		entry = this->m_ParentTable->Find(i_Name);
	}

	return entry;
}

bool SymbolTable::Exists(char *i_Name) const
{
	return this->Find(i_Name) != NULL;
}

SymbolTableEntry *SymbolTable::operator[] (char *i_Key)
{
	return this->Find(i_Key);
}